//控制层 
app.controller('courseController', function($scope, $controller, courseService, $rootScope, $timeout) {

	$controller('baseController', {
		$scope: $scope
	}); //继承

	//读取列表数据绑定到表单中  
	$scope.findAll = function() {
		courseService.findAll().success(
			function(response) {
				$scope.list = response;
			}
		);
	}

	//分页
	$scope.findPage = function(page, rows) {
		courseService.findPage(page, rows).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	$scope.entity = {};

	//查询实体 
	$scope.findOne = function(id) {
		courseService.findOne(id).success(
			function(response) {
				$scope.entity = response;
			}
		);

	}

	//保存 
	$scope.save = function() {
		var serviceObject; //服务层对象  				
		if($scope.entity.cId != null) { //如果有ID
			serviceObject = courseService.update($scope.entity); //修改  
		} else {
			serviceObject = courseService.add($scope.entity); //增加 
		}
		serviceObject.success(
			function(response) {
				if(response.success) {
					alert(response.message);
					window.location = "course-course.html";
					//重新查询
					//$scope.reloadList(); //重新加载
				} else {
					alert(response.message);
				}
			}
		);
	}

	//批量删除  
	$scope.dele = function() {
		//获取选中的复选框			
		courseService.dele($scope.selectIds).success(
			function(response) {
				if(response.success) {
					$scope.reloadList(); //刷新列表
					$scope.selectIds = [];
				}
			}
		);
	}

	//单个删除
	$scope.delsFlag = false;
	$scope.delsingle = function(cid) {
		courseService.dele(cid).success(
			function(response) {
				if(response.success) {
					$scope.findAll();
					$scope.delsFlag = true;
					$timeout(function() {
						$scope.delsFlag = false;
					}, 2000);
				}
			}
		);
	}
	$scope.searchEntity = {}; //定义搜索对象 

	//搜索
	$scope.search = function(page, rows) {
		courseService.search(page, rows, $scope.searchEntity).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	//编辑课程按钮
	$scope.editCourse = function(id) {
		console.log(id);
		courseService.findOne(id).success(
			function(response) {
				$scope.data = response;
				$scope.entity.cName = $scope.data.cName;
				angular.element('#editCourse').click();
			}
		);
	}

	//上传图片并保存课程
	$scope.uploadAndSave = function() {
		//提交课程前先保存图片到数据库,并拿到相对路径		
		if($scope.entity.image != null) {
			courseService.upload().then(function(response) {
				//保存图片后进行课程信息和图片路径的保存
				$scope.entity.cPicture = response.data.data.src;
				$scope.save();
			});
		} else {
			$scope.save();
		}
	}

	//修改
	$scope.edit = function($index, cId) {
		$scope.xs = true;
		$scope.entity.cId = cId;
		$scope.entity.cName = $scope.list[$index].cName;
		$scope.entity.cIntroduction = $scope.list[$index].cIntroduction;
		$scope.entity.cPicture = $scope.list[$index].cPicture;
		$scope.editIndex = $index;
	}
	$scope.editOk = function() {
		var entity = {
			cName: $scope.entity.cName,
			cIntroduction: $scope.entity.cIntroduction,
			cPicture: $scope.entity.cPicture
		}
		courseService.update(entity).then(function(response) {
			//保存图片后进行课程信息和图片路径的保存
			window.location = "course-course.html";
			$scope.findAll();
		});
	}

	//选中全部
	$scope.selectAll = function($event) {
		if($event.target.checked) { //如果是被选中,则增加到数组
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				$scope.selectIds.push(each.cId); //将空数组中push新的数据，符合条件的数据的id值
			});
		} else {
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				var idx = $scope.selectIds.indexOf(each.cId);
				$scope.selectIds.splice(idx, 1); //删除 
			});
		}

	}

});